<?php include("header.php"); ?>

<script type="text/javascript">
    function validate()
	{
        var email = document.getElementById('email');
        var first_name = document.getElementById('first_name');
        var last_name = document.getElementById('last_name');
        var id_group = document.getElementById('id_group');
        var password = document.getElementById('password');
        var deactived = document.getElementById('deactived');
        var confirm_password = document.getElementById('confirm_password');
		
        if(isEmptyTextBox(first_name, '<?php echo _LB_FIRST_NAME_IS_EMPTY; ?>'))
        {
            return false;
        }
        
        if(first_name.value.length > '<?php echo MAX_LENGTH_NAME; ?>')
        {
            showErrorBubble(first_name, '<?php echo _LB_FIRST_NAME_IS_TOO_LONG; ?>');
            return false;
        }
        
        if(isEmptyTextBox(last_name, '<?php echo _LB_NAME_IS_EMPTY; ?>'))
        {
            return false;
        }
        
        if(last_name.value.length > '<?php echo MAX_LENGTH_NAME; ?>')
        {
            showErrorBubble(last_name, '<?php echo _LB_NAME_IS_TOO_LONG; ?>');
            return false;
        }
        
        <?php if($user->id != $this->session->userdata('user_id')) {?>
        if(isEmptyTextBox(email, '<?php echo _LB_EMAIL_IS_EMPTY; ?>'))
        {
            return false;
        }
        
        if(email.value.length > '<?php echo MAX_LENGTH_NAME; ?>')
        {
            showErrorBubble(email, '<?php echo _LB_EMAIL_IS_TOO_LONG; ?>');
            return false;
        }
        var agr = new Array();
        agr.push(trim(email.value));
        agr.push('<?php echo $user->id;?>');

        if(callFunction("User::emailExistByUserID", agr)!='0')
        {
            showErrorBubble(email, '<?php echo _MSG_EMAIL_EXISTS; ?>');
            return false;
        }
        <?php } ?>

        <?php if(!$user->id) { ?>
        if(isEmptyTextBox(password, '<?php echo _LB_INVALID_PASSWORD; ?>'))
        {
            return false;
        }
        
        if(password.value.length > '<?php echo MAX_LENGTH_PASSWORD; ?>')
        {
            showErrorBubble(password, '<?php echo _MSG_PASSWORD_IS_TOO_LONG; ?>');
            return false;
        }
        
        if(password.value.length < '<?php echo MIN_LENGTH_PASSWORD; ?>')
        {
            showErrorBubble(password, '<?php echo _MSG_PASSWORD_IS_TOO_SHORT; ?>');
            return false;
        }
        
        if(isEmptyTextBox(confirm_password, '<?php echo _LB_INVALID_CONFIRM_PASSWORD; ?>'))
        {
            return false;
        }
        
        if (password.value != confirm_password.value) {
            showErrorBubble(confirm_password, '<?php echo _LB_PASSWORD_DOESNT_MATCH; ?>');
            return false;
        }
        <?php } ?>
        
        <?php if($this->session->userdata('group_id') == ADMIN) {?>
        if(isEmptyTextBox(id_group, '<?php echo _LB_ACCOUNT_TYPE_IS_EMPTY; ?>'))
        {
            return false;
        }
        <?php } ?>
        
        document.user.submit();
	}

</script>

	<div class="one_center">
		<div class="box_two_third">
			<div class="box_head"><?php if($user->id) { echo _LB_EDIT_USER;}else{echo _LB_ADD_USER;} ?></div>
			<div class="box_content" >
				<form action="" name="user" method="post">
				<input type="hidden" id="id" name="id" value="<?php $user->id; ?>"/>
				<input type="hidden" id="act" name="act" value="<?php echo ACT_VALID_EDIT; ?>"/>
				<table cellpadding="0" cellspacing="5px" border="0">
                    <tr>
						<td width="20%"><label><?php echo _LB_EMAIL; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></label></td>
                        <td>
                            <?php if($user->id == $this->session->userdata('user_id')) {?> <?php echo $user->email; ?><?php } else { ?>
                            <input name="email" id="email" type="text" size="<?php echo SIZE_NAME; ?>" maxlength="<?php echo MAX_LENGTH_NAME;?>" value="<?php echo $user->email; ?>"/><?php } ?>
                        </td>
					</tr>
                    <tr>
						<td width="20%"><label><?php echo _LB_FIRST_NAME; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></label></td>
                        <td><input name="first_name" id="first_name" type="text" size="<?php echo SIZE_NAME; ?>" maxlength="<?php echo MAX_LENGTH_NAME;?>" value="<?php echo $user->first_name; ?>"/></td>
					</tr>
                    <tr>
						<td width="20%"><label><?php echo _LB_NAME; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></label></td>
                        <td><input name="last_name" id="last_name" type="text" size="<?php echo SIZE_NAME; ?>" maxlength="<?php echo MAX_LENGTH_NAME;?>" value="<?php echo $user->last_name; ?>"/></td>
					</tr>
                    
                    <?php if(!$user->id) { ?>
                    <tr>
						<td width="20%"><label><?php echo _LB_PASSWORD; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></label></td>
                        <td><input name="password" id="password" type="password" size="<?php echo SIZE_NAME; ?>" maxlength="<?php echo MAX_LENGTH_NAME;?>" value="<?php echo $user->pass; ?>"/></td>
					</tr>
                    <tr>
						<td width="20%"><label><?php echo _LB_REPEAT_PASSWORD; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></label></td>
                        <td><input name="confirm_password" id="confirm_password" type="password" size="<?php echo SIZE_NAME; ?>" maxlength="<?php echo MAX_LENGTH_NAME;?>" value=""/></td>
					</tr>
                    <?php } ?>
                    <?php if ($this->session->userdata('id_group') == ADMIN && $user->id != $this->session->userdata('user_id'))
                    { ?>
                    <tr>
                        <td width="40%"><?php echo _LB_USER_TYPE; ?>&nbsp;:&nbsp;<?php echo _LB_REQUIRE_FIELD; ?></td>
                        <td width="60%">
                            <select name="id_group" id="id_group" style="width: 185px;">
                                <option value=""><?php echo _LB_CHOOSE; ?></option>
                                <?php while($group->fetchNext()) { ?>
                                   <option value="<?php echo $group->id; ?>" <?php if($group->id == $user->id_user_group) {?>selected="true"<?php }?>><?php echo $group->name; ?></option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    <?php } ?>
                    <tr>
					  	<td class="submit" colspan="2" style="padding-top: 10px;">
							  <a id="auto_port_return" class="tribullet" id="btnReturn" href="<?php echo $backlink; ?>"><?php echo _LB_BACK; ?></a>
							  <a id="auto_port_validate" class="tribullet" href="#" onclick="validate()"><?php echo _LB_VALIDATE; ?></a>
						</td>
					</tr>
				</table>
				</form>
			</div>
		</div>
	</div>

<?php include("footer.php"); ?>

